using System.Collections.Generic;
using OhioState.AssetManaged;
using OhioState.Graphics;


namespace OhioState.Graphics.OpenGL
{
    public class AssetManagerGL : IRenderResourceManager
    {
        private Dictionary<IAssetManaged, OpenGLObject> handlersForAssets = new Dictionary<IAssetManaged, OpenGLObject>();
        /// <summary>
        /// Get the handler object that corresponds to the asset.
        /// </summary>
        /// <param name="asset">The asset (or proxy object) for which a real object should be retrieved.</param>
        /// <returns>The corresponding handler instance.</returns>
        /// <remarks>The first time GetItem is called for an asset, it will be created and stored in this asset manager.
        /// All subsequent requests for the exact same asset will return this instance.</remarks>
        public object GetItem(IAssetManaged asset)
        {
            if (asset == null)
                return null;
            if (handlersForAssets.ContainsKey(asset))
            {
                return handlersForAssets[asset];
            }
            else if (asset == null)
                return null;
            else
            {
                OpenGLObject realObj = GLFactory.OpenGLFactory(asset);
                handlersForAssets.Add(asset, realObj);
                return realObj;
            }
        }

        /// <summary>
        /// Iterate through all assets (handlers)
        /// and remove their change handlers from corresponding proxies 
        /// by calling each asset's dispose method.
        /// </summary>
        public void Dispose()
        {        
            //really clumsy way of doing things - temporary hack
            foreach (OpenGLObject asset in handlersForAssets.Values)
            {
                asset.Dispose();
            }
        }
    }
}
